import 'dart:async';
import 'package:bot_toast/bot_toast.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'dart:convert';
import '../common_widget/countdown_button_page.dart';
import '../common_widget/hexcolor.dart';
import '../common_widget/phone_inputformatter.dart';
import '../home_module/home_page.dart';
import '../net/address.dart';
import '../net/dio_manager.dart';
import '../net/persisten_storage.dart';
import 'login_model.dart';


class CodeLoginPage extends StatefulWidget {
  const CodeLoginPage({Key? key}) : super(key: key);

  @override
  _CodeLoginPageState createState() => _CodeLoginPageState();
}

class _CodeLoginPageState extends State<CodeLoginPage>
    with SingleTickerProviderStateMixin {

  var _username = TextEditingController();

  var _pwd = TextEditingController();

  var options = '+86';


  requestDataWithCodeLogin(var login,var password)async{

    var params = {
      'login':login,
      'code':password,
      'type':'1'
    };

    var json = await DioManager().kkRequest(Address.login,bodyParams: params,isShowLoad: true);

    Usesr model = Usesr.fromJson(json);

    if (model.errmsg == 'ok') {
      // await prefs.setString('acctoken', _model.data.accessToken);
      PersistentStorage().setStorage('acctoken', model.data!.accessToken);
      PersistentStorage().setStorage('uid', model.data!.uid);
      PersistentStorage().setStorage('partner_id', model.data!.partnerId);
      PersistentStorage().setStorage('image128', model.data!.image128);
      PersistentStorage().setStorage('job_company_id', model.data!.jobCompanyId.toString());
      PersistentStorage().setStorage('job_user_id', model.data!.jobUserId.toString());
      PersistentStorage().setStorage('name', model.data!.name);
      PersistentStorage().setStorage('companyIdName', model.data!.companyIdName);
      PersistentStorage().setStorage('companyId', model.data!.companyId.toString());

      Get.offAll(const HomePage());

    } else {
      BotToast.showText(text: json['message']);
    }

  }



  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView(
        children: [
          Center(
            child: Container(
                decoration: new BoxDecoration(
                  // color: Colors.white,
                  //设置四周圆角 角度
                  borderRadius: BorderRadius.all(Radius.circular(25.0)),
                  //设置四周边框
                  border: new Border.all(
                      width: 0.5, color: Colors.white),
                ),
                margin: EdgeInsets.only(top: 20, left: 15, right: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(

                        margin: EdgeInsets.only(left: 15),
                        width: 200,
                        child: Row(
                          children: [

                            GestureDetector(
                              onTap: () async {
                                // var data = await  Get.to(SelectOptionsPage());
                                // if(data!=null){
                                //   options = data;
                                //   setState(() {
                                //
                                //   });
                                // }

                              },
                              child: Container(
                                margin: EdgeInsets.only(top: 2),
                                // color: Colors.red,
                                child: Text(
                                  '${options}', style: TextStyle(
                                    color: Colors.white),),
                              ),
                            ),
                            // TextButton(onPressed: (){
                            //   Get.to(SelectOptionsPage());
                            //
                            // }, child: Text('+86')),

                            Container(
                              padding: EdgeInsets.only(left: 5),
                              width: 160,
                              child: TextField(

                                controller: _username,
                                keyboardType: TextInputType.phone,
                                style: TextStyle(
                                    fontSize: 16, color: Colors.white),
                                inputFormatters: <TextInputFormatter>[
                                  LengthLimitingTextInputFormatter(13), //限制长度
                                  phoneInputFormatter(),
                                ],
                                decoration: InputDecoration(
                                  hintText: '请输入手机号码'.tr,
                                  hintStyle: TextStyle(fontSize: 16,

                                      color: Colors.white.withAlpha(50)),
                                  border: InputBorder.none,
                                  // prefixIcon: Icon(Icons.phone_android,size: 15,),
                                ),
                              ),
                            )
                          ],
                        )
                    ),
                    CountdownButton(_username),
                  ],
                )
            ),
          ),
          Center(
            child: Container(
              decoration: new BoxDecoration(
//背景
//                 color: Colors.white,
                //设置四周圆角 角度

                borderRadius: BorderRadius.all(Radius.circular(25.0)),
                //设置四周边框
                border: new Border.all(width: 0.5, color: Colors.white),


              ),

              margin: EdgeInsets.only(top: 10, right: 15, left: 15),
              child: Container(
                margin: EdgeInsets.only(left: 25),
                child: TextField(
                  inputFormatters: <TextInputFormatter>[
                    LengthLimitingTextInputFormatter(6)//限制长度
                  ],
                  controller: _pwd,
                  style: TextStyle(fontSize: 16, color:Colors.white),
                  decoration: InputDecoration(
                    hintText: '请输入验证码'.tr,
                    hintStyle: TextStyle(fontSize: 16, color: Colors.white.withAlpha(50)),
                    border: InputBorder.none,
                    // prefixIcon: Image.asset(A.assets_pwd),
                  ),
                ),
              ),
            ),
          ),


          // Container(
          //   padding: EdgeInsets.only(left: 25, top: 15, right: 25),
          //   child: Row(
          //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //     children: [
          //       GestureDetector(
          //
          //         child: Text(
          //           '手势登录'.tr, style: TextStyle(fontSize: 14, color: HexColor(
          //             '#2A39E1')),),
          //         onTap: () {
          //           // Get.to(GesturePasswordWidgetDemo());
          //         },
          //       ),
          //       GestureDetector(
          //
          //         child: Text(
          //           '人脸登录'.tr, style: TextStyle(fontSize: 14, color: HexColor(
          //             '#2A39E1')),),
          //         onTap: () {
          //           // Get.to(FaceLoginPage());
          //         },
          //       ),
          //     ],
          //   ),
          // ),

          Container(
            margin: EdgeInsets.only(top: 72, right: 15, left: 15),
            width: Get.width,
            height: 45,
            child: MaterialButton(
              elevation: 0,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(22),
              ),
              color: Theme
                  .of(context)
                  .primaryColor,
              onPressed: () async {

                if (_username.text.isEmpty) {
                  // Toast.toast(context,msg: '请输入手机号码');
                  BotToast.showText(text: '请输入手机号码');
                  return;
                }
                if (_pwd.text.isEmpty) {
                  // Toast.toast(context,msg: '请输入手机号码');
                  BotToast.showText(text: '请输入验证码');
                  return;
                }
                if(!GetUtils.isPhoneNumber(_username.text)){
                  BotToast.showText(text: '请输入正确的手机号码');
                  return;
                }

                requestDataWithCodeLogin(_username.text.replaceAll(new RegExp(r"\s+\b|\b\s"), ""), _pwd.text);
                },
              child: Text('登录'.tr, style: TextStyle(color: Colors.white),),
            ),
          ),
        ],
      ),
    );
  }
}


